package com.example.demo6.ui.edit;

import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.demo6.R;
import com.example.demo6.SQLite.LostGoods;
import com.example.demo6.SQLite.MyDatabase;
import com.example.demo6.ui.detail.losedetailFragment;

public class edit_mylostfragment extends Fragment {

    private EditText editMylostItem,editMylostPlace,editMylostPhone,editMylostTime,editMylostItemDetail;
    private Button UpdateMylostBtn,CancelMylostBtn,DeleteMylostBtn;
    LostGoods lostGoods;

    private EditMylostfragmentViewModel mViewModel;

    private NavController navController;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_mylostfragment, container, false);

        // 初始化UI组件
        editMylostItem = view.findViewById(R.id.editMylostItem);
        editMylostPlace = view.findViewById(R.id.editMylostPlace);
        editMylostPhone = view.findViewById(R.id.editMylostPhone);
        editMylostTime = view.findViewById(R.id.editMylostTime);
        editMylostItemDetail = view.findViewById(R.id.editMylostItemDetail);
        UpdateMylostBtn = view.findViewById(R.id.UpdateMylostBtn);
        CancelMylostBtn = view.findViewById(R.id.CancelMylostBtn);
        DeleteMylostBtn = view.findViewById(R.id.DeleteMylostBtn);


        // 从Bundle中获取goodsId
        int goodsId = getArguments() != null ? getArguments().getInt("goodsId") : -1;
        if (goodsId != -1) {
            new SelectMyLostGoodsTask(goodsId).execute();
        }

        // 设置更新按钮的点击监听器
        if (UpdateMylostBtn != null) {
            UpdateMylostBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateLostGoods();
                }
            });
        }

        // 设置取消按钮的点击监听器
        if (CancelMylostBtn != null) { // 添加空值检查
            CancelMylostBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NavController navController = Navigation.findNavController((Activity) getContext(), R.id.nav_host_fragment_activity_main);
                    navController.popBackStack();
                }
            });
        }

        // 设置删除按钮的点击监听器
        if (DeleteMylostBtn != null) {
            DeleteMylostBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteLostGoods();
                }
            });
        }

        return view;
    }

    private void updateLostGoods() {
        // 检查lostGoods是否已加载
        if (lostGoods != null) {
            // 更新LostGoods对象的字段
            lostGoods.setLostGoods(editMylostItem.getText().toString());
            lostGoods.setLostPlace(editMylostPlace.getText().toString());
            lostGoods.setLoserPhone(editMylostPhone.getText().toString());
            lostGoods.setLostTime(editMylostTime.getText().toString());
            lostGoods.setLostDetail(editMylostItemDetail.getText().toString());

            // 执行更新操作
            new UpdateMyLostGoodsTask().execute(lostGoods);
        } else {
            Toast.makeText(getContext(), "数据加载失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }

    private void deleteLostGoods() {
        // 检查lostGoods是否已加载
        if (lostGoods != null) {
            // 执行删除操作
            new DeleteMyLostGoodsTask().execute(lostGoods.getGoodsId());
        } else {
            Toast.makeText(getContext(), "数据加载失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
    @SuppressLint("StaticFieldLeak")
    private class SelectMyLostGoodsTask extends AsyncTask<Void, Void, Void>
    {
        int goodsId;
        public SelectMyLostGoodsTask(int goodsId)
        {
            this.goodsId=goodsId ;
        }
        //不能在这里更新UI,否则有异常
        @Override
        protected Void doInBackground(Void... arg0)
        {
            lostGoods= MyDatabase.getInstance(getContext()).lostGoodsDao().getMyLostGoodsByGoodsId(goodsId);
            return null;
        }
        //onPostExecute用于doInBackground执行完后，可以更新界面UI。
        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            // 绑定数据到视图
            bindDataToView(lostGoods);
        }
    }

    //更新
    @SuppressLint("StaticFieldLeak")
    private class UpdateMyLostGoodsTask extends AsyncTask<LostGoods, Void, Void> {
        @Override
        protected Void doInBackground(LostGoods... lostGoods) {
            MyDatabase.getInstance(getContext()).lostGoodsDao().updateMyLostGoods(lostGoods[0]);
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Toast.makeText(getContext(), "更新成功！", Toast.LENGTH_SHORT).show();
            NavController navController = Navigation.findNavController((Activity) getContext(), R.id.nav_host_fragment_activity_main);
            navController.popBackStack();
        }
    }
    //删除
    @SuppressLint("StaticFieldLeak")
    private class DeleteMyLostGoodsTask extends AsyncTask<Integer, Void, Void> {
        @Override
        protected Void doInBackground(Integer... integers) {
            MyDatabase.getInstance(getContext()).lostGoodsDao().deleteMyLostGoodsById(integers[0]);
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Toast.makeText(getContext(), "删除成功！", Toast.LENGTH_SHORT).show();
            NavController navController = Navigation.findNavController((Activity) getContext(), R.id.nav_host_fragment_activity_main);
            navController.popBackStack();
        }
    }
    private void bindDataToView(LostGoods lostGoods) {
        editMylostItem.setText(lostGoods.getLostGoods());
        editMylostPlace.setText(lostGoods.getLostPlace());
        editMylostPhone.setText(lostGoods.getLoserPhone());
        editMylostTime.setText(lostGoods.getLostTime());
        editMylostItemDetail.setText(lostGoods.getLostDetail());
    }
}
